package com.cms.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.action.DefaultAction;
import com.cms.manager.LanguageManager;
import com.cms.manager.TypeManager;
import com.cms.model.Language;
import com.cms.model.Message;
import com.cms.model.Type;
import com.cms.service.MessageService;
import com.cms.service.MessageServiceImpl;

public class MessageAction extends DefaultAction {

	public String show()
	{
		if(msg.equals("update"))
		{
			message = service.getById(message.getId());
		}
		return SUCCESS;
	}
	
	public String save()
	{
		if(msg.equals("insert"))
		{
			if(upload!=null)
			{
				String rfileName = new Date().getTime()+"";
				message.setFileName(uploadFileName);
				message.setRfileName(rfileName);
				message.setFilePath(uploadFile(upload,rfileName));
			}
			if(service.insert(message))
			{
				msg="内容管理：添加内容成功！！";
			}
			else
			{
				msg="内容管理：对不起，添加内容可能存在问题，请稍候重试一下！！";
			}
		}
		else if(msg.equals("update"))
		{
			if(upload!=null)
			{
				String rfileName = new Date().getTime()+"";
				message.setFileName(uploadFileName);
				message.setRfileName(rfileName);
				message.setFilePath(uploadFile(upload,rfileName));
			}
			else
			{
				Message m = service.getById(message.getId());
				message.setFileName(m.getFileName());
				message.setFilePath(m.getFilePath());
				message.setRfileName(m.getRfileName());
			}
			if(service.update(message))
			{
				msg="内容管理：更新内容成功！！";
			}
			else
			{
				msg="内容管理：对不起，更新内容可能存在问题，请稍候重试一下！！";
			}
		}
		else if(msg.equals("del"))
		{
			if(service.delete(message))
			{
				msg="内容管理：删除内容成功！！";
			}
			else
			{
				msg="内容管理：对不起，删除内容可能存在问题，请稍候重试一下！！";
			}
		}
		return SUCCESS;
	}
	public String download()
	{
		try {
			HttpServletResponse response = super.getResponse();
			Message m = service.getById(message.getId());
			InputStream is = new BufferedInputStream(new FileInputStream(m.getFilePath()+m.getRfileName()));
			byte[] b = new byte[is.available()];
			is.read(b);
			is.close();
			
			String fileName = m.getFileName();
	    	response.setContentType("application/octet-stream");
	    	response.setHeader("Location", fileName);
	    	response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
	    	OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
	    	outputStream.write(b);
	    	outputStream.flush();
	    	outputStream.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	public String list()
	{
		mlist = service.getAll(message);
		return SUCCESS;
	}
	
	private String uploadFile(File f,String fileName)
	{
		String path = "";
		try {
			if(f!=null)
			{
				path=super.getRealPath("/")+"uploadfile//";
				File ff = new File(path);
				if(!ff.exists())
				{
					ff.mkdirs();
				}
				super.saveUploadFile(f, path+fileName);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			path="";
		}
		return path;
	}
	public List<Language> getAllLanguage()
	{
		return LanguageManager.getInstance().getAll();
	}
	public List<Type> getAllType()
	{
		return TypeManager.getInstance().getAll();
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1940882605107147866L;

	private String msg="";
	private String uploadFileName="";
	private File upload=null;
	private Message message = new Message();
	private List<Message> mlist = new ArrayList<Message>();
	private MessageService service = new MessageServiceImpl();
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Message getMessage() {
		return message;
	}
	public void setMessage(Message message) {
		this.message = message;
	}
	public List<Message> getMlist() {
		return mlist;
	}
	public void setMlist(List<Message> mlist) {
		this.mlist = mlist;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}
	
	
}
